#include "rclcpp/rclcpp.hpp"
#include "base_interfaces_demo/srv/add_ints.hpp"
using base_interfaces_demo::srv::AddInts;
using namespace std::chrono_literals;
class AddIntsClient :public rclcpp::Node
{
public:
  AddIntsClient():Node("addints_client_cpp_node")
  {
    RCLCPP_INFO(this->get_logger(),"客户端节点已创建");
    this->_client=this->create_client<AddInts>("addints");
    RCLCPP_INFO(this->get_logger(),"客户端已创建");
  }
  rclcpp::Client<AddInts>::FutureAndRequestId send_Request(int64_t num1,int64_t num2)
  {
    while(!_client->wait_for_service(1s))
    {
      if(!rclcpp::ok())
      {
        RCLCPP_WARN(rclcpp::get_logger("rclcpp"),"已强制退出！！！");
        exit(1);
      }
      else
      {
        RCLCPP_INFO(this->get_logger(),"正在连接服务器！！！");
      }
    }
    auto request=std::make_shared<AddInts::Request>();
    request->num1=num1;
    request->num2=num2;
    return _client->async_send_request(request);
  }
private:
  rclcpp::Client<AddInts>::SharedPtr _client;  
};

int main(int argc,char*argv[])
{
  if(argc!=3)
  {
    RCLCPP_ERROR(rclcpp::get_logger("rclcpp"),"请输入两个整形数字！！！");
    exit(1);
  }
  rclcpp::init(argc,argv);
  auto addintsclient=std::make_shared<AddIntsClient>();
  rclcpp::Client<AddInts>::FutureAndRequestId future=addintsclient->send_Request(atoi(argv[1]),atoi(argv[2]));
  if(rclcpp::spin_until_future_complete(addintsclient,future)==rclcpp::FutureReturnCode::SUCCESS)
  {
    RCLCPP_INFO(addintsclient->get_logger(),"响应的结果是：%d",future.get()->sum);
  }
  else
  {
     RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"响应失败！！!");
  }
  rclcpp::shutdown();
  return 0;
}